'use client';
import dayjs from 'dayjs';
import { fetchWrapper } from './common';

import { getUrlParams } from './baseInfo';

const pathMap = {
  dev: '/dev-api',
  staging: '/uat-api',
  prod: '/prod-api',
};
const env = (process.env.NEXT_PUBLIC_APP_ENV || 'prod') as
  | 'dev'
  | 'staging'
  | 'prod';

const envTest =
  typeof window !== 'undefined' &&
  (new URL(window.location.href).searchParams.get('env') as typeof env);

console.log(envTest, 'envTest');
const envFinale = env == 'prod' ? env : envTest || env;
const path = pathMap[envFinale];

const wmsPath = path;
const reportPath = path + "-report";
const warehouseId = getUrlParams('warehouseId');
const warehouseIds = [warehouseId];
const tenantId = getUrlParams('tenantId');

export type WarehouseData = {
  monitorServerAccount?: string;
  monitorServerAddr?: string;
  monitorServerPassword?: string;
  monitorServerSecret?: string;
  tenantId?: string;
  tenantName?: string;
  warehouseId?: number;
  warehouseName?: string;
  monitorPlatform?: 'Dahua' | 'HIKVISION' | 'YINGSY';
  otherDataMap?:{accessToken: string, areaDomain: string}
};

export function getWarehouseList(warehouseId?:string) {
  return fetchWrapper<{
    warehouseList: WarehouseData[];
    earthParams: string;
    warehouseParams: string;
    gylParams: string; // 仓库地图参数
  }>(`${wmsPath}/wms/warehouse/banner-warehouse-v2`, undefined, 'GET');
}

/**
 * WarehouseCameraQuery
 */
export interface CameraRequest {
  /**
   * 摄像头频道channel: 頻道, 1-32 精确匹配
   */
  cameraChannel?: null | string;
  /**
   * 摄像头编码 精确匹配
   */
  cameraCode?: null | string;
  /**
   * 摄像头封面地址 精确匹配
   */
  cameraCoverUrl?: null | string;
  /**
   * 摄像头mac地址 精确匹配
   */
  cameraMac?: null | string;
  /**
   * 摄像头名称 精确匹配
   */
  cameraNameLike?: null | string;
  /**
   * 摄像头网络地址 精确匹配
   */
  cameraNetworkAddress?: null | string;
  /**
   * 摄像头位置 精确匹配
   */
  cameraPosition?: null | string;
  /**
   * 摄像头是否展示：0可展示，1不可展示
   */
  cameraShow?: number | null;
  /**
   * 摄像头可用状态：0可用，1不可用
   */
  cameraStatus?: number | null;
  /**
   * 摄像头码流类型：主碼流 0, 輔碼流 1. 精确匹配
   */
  cameraSubType?: number | null;
  /**
   * 摄像头品牌： Dahua,Haikang 精确匹配
   */
  cameraVendor?: null | string;
  /**
   * 租户ID 精确匹配
   */
  tenantId?: null | string;
  /**
   * 链路ID 精确匹配
   */
  traceId?: null | string;
  /**
   * 仓库ID 精确匹配
   */
  warehouseId?: number | null;
}

/**查询仓库摄像头设备列表 */
export function getCameraListApi() {
  return fetchWrapper<{
    content: CameraRequest[];
  }>(
    `${wmsPath}/wms/warehouseCamera/list?page=0&size=100`,
    { warehouseId, tenantId },
    'POST',
  );
}

/**
 * 库存 DTO 对象
 *
 * InventoryDTO
 */
export interface InventoryDTO {
  /**
   * 库区号
   */
  areaNO?: null | string;
  /**
   * 货主id
   */
  customerId?: null | string;
  /**
   * 货主名称
   */
  customerName?: null | string;
  /**
   * 库位号
   */
  locationNO?: null | string;
  /**
   * pcs数量
   */
  quantity?: number | null;
  /**
   * 备注
   */
  remark?: null | string;
  /**
   * sku编码
   */
  skuCode?: null | string;
  /**
   * sku种类数量
   */
  skuCountNum?: number | null;
  /**
   * sku中文名称
   */
  skuNameCn?: null | string;
  /**
   * sku英文名称
   */
  skuNameEn?: null | string;
  /**
   * 租户id
   */
  tenantId?: null | string;
  /**
   * 仓库id
   */
  warehouseId?: null | string;
  /**
   * 库龄
   */
  inventoryAges?: string;
}

/**
 * InventoryVO
 */
export interface InventoryVO {
  /**
   * 货位维度库存清单
   */
  locationVertoryList?: InventoryDTO[] | null;
  /**
   * sku维度库存清单
   */
  skuVertoryList?: InventoryDTO[] | null;
  inventorySkuPcs?: number;
  inventorySkuSpecs?: number;
}

let urlMonthStr =
  typeof window !== 'undefined' &&
  new URL(window.location.href).searchParams.get('month');

const urlMonth = urlMonthStr ? parseInt(urlMonthStr) : 12;

/**库存统计-sku维度、库位维度 */
export function locationOrSkuApi() {
  const startDate = dayjs()
    .subtract(urlMonth, 'month')
    .format('YYYY-MM-DD HH:mm:ss');
  const endTDate = dayjs().format('YYYY-MM-DD HH:mm:ss');

  return fetchWrapper<InventoryVO>(
    `${reportPath}/report/large-screen/statistics-locationOrSku?page=0&size=1000`,
    {
      warehouseIds,
      tenantId,
      receiptEndTime: endTDate,
      receiptStartTime: startDate,
      shipmentEndTime: endTDate,
      shipmentStartTime: startDate,
    },
    'POST',
  );
}

/**
 * LocationSpaceUsageVO
 */
export interface LocationSpaceUsageVO {
  /**
   * 备货位使用率
   */
  backupLocationList?: WarehouseReportDayDTO[] | null;
  /**
   * 拣货位使用率
   */
  pickLocationList?: WarehouseReportDayDTO[] | null;
  /**
   * 总货位使用率
   */
  totalLocationList?: WarehouseReportDayDTO[] | null;
}

/**
 * com.zjygwms.report.pojo.dto.WarehouseReportDayDTO
 *
 * WarehouseReportDayDTO
 */
export interface WarehouseReportDayDTO {
  /**
   * 创建时间
   */
  createTime?: null | string;
  /**
   * 日
   */
  day?: null | string;
  /**
   * 删除标志（0代表存在 2代表删除）
   */
  delFlag?: number | null;
  /**
   * 动态维度1
   */
  dycDim1?: null | string;
  /**
   * 动态维度2-货位使用率
   */
  dycDim2?: null | string;
  /**
   * 动态维度3
   */
  dycDim3?: null | string;
  /**
   * 动态维度4
   */
  dycDim4?: null | string;
  /**
   * 动态维度5
   */
  dycDim5?: null | string;
  /**
   * 动态维度6
   */
  dycDim6?: null | string;
  /**
   * ID
   */
  id?: number | null;
  /**
   * 月
   */
  month?: null | string;
  /**
   * 值1
   */
  quantity1?: number | null;
  /**
   * 值2
   */
  quantity2?: number | null;
  /**
   * 值3
   */
  quantity3?: number | null;
  /**
   * 值4
   */
  quantity4?: number | null;
  /**
   * 值5
   */
  quantity5?: number | null;
  /**
   * 值6
   */
  quantity6?: number | null;
  /**
   * 值7
   */
  quantity7?: number | null;
  /**
   * 季
   */
  quarter?: null | string;
  /**
   * 备注
   */
  remark?: null | string;
  /**
   * 报表名称
   */
  reportName?: null | string;
  /**
   * 租户id
   */
  tenantId?: null | string;
  /**
   * 租户名称
   */
  tenantName?: null | string;
  /**
   * 时区
   */
  timeZone?: null | string;
  /**
   * 单位
   */
  unit?: null | string;
  /**
   * 仓库id
   */
  warehouseId?: null | string;
  /**
   * 仓库名称
   */
  warehouseName?: null | string;
  /**
   * 周
   */
  week?: null | string;
  /**
   * 年
   */
  year?: null | string;
}

/**货位使用率统计-总货位、拣货位、备货位 */
export function locationSpaceUsageApi() {
  const startDate = dayjs()
    .subtract(urlMonth, 'month')
    .format('YYYY-MM-DD HH:mm:ss');
  const endTDate = dayjs().format('YYYY-MM-DD HH:mm:ss');
  return fetchWrapper<LocationSpaceUsageVO>(
    `${reportPath}/report/large-screen/statistics-locationSpaceUsage`,
    {
      warehouseIds,
      tenantId,
      receiptEndTime: endTDate,
      receiptStartTime: startDate,
      shipmentEndTime: endTDate,
      shipmentStartTime: startDate,
    },
    'POST',
  );
}

/**
 * StatisticsOrderVO
 */
export interface StatisticsOrderVO {
  /**实时仓库总库存sku种类数量" */
  inventorySkuSpecs: number;
  /**实时仓库总库存pcs数" */
  inventorySkuPcs: number;
  /**
   * 总接单量-入库
   */
  receiptAddNum?: number | null;
  /**
   * 已收货量
   */
  receiptReceiveNum?: number | null;
  /**
   * 入库趋势数据
   */
  receiptTendencyList?: StatisticsOrderDTO[] | null;
  /**
   * 已上架量
   */
  receiptUploadNum?: number | null;
  /**
   * 总接单量-出库
   */
  shipmentAddNum?: number | null;
  /**
   * 已发货量
   */
  shipmentDeliverNum?: number | null;
  /**
   * 已出库量
   */
  shipmentOutNum?: number | null;
  /**
   * 已拣选量
   */
  shipmentPickNum?: number | null;
  /**
   * 出库趋势数据
   */
  shipmentTendencyList?: StatisticsOrderDTO[] | null;
  otherMaps: { [key: string]: any };
}

/**
 * StatisticsOrderDTO
 */
export interface StatisticsOrderDTO {
  /**
   * 仓库-操作时间-操作名称合并对应的单据数量
   */
  countNum?: number | null;
  /**
   * 操作名称
   */
  operLog?: null | string;
  /**
   * 操作名称-转换后名称
   */
  operLogConvert?: null | string;
  /**
   * 操作时间-天
   */
  operTimeDay?: null | string;
  /**
   * 操作时间-小时
   */
  operTimeHour?: null | string;
  /**周 */
  operTimeWeek?: null | string;
  /**
   * 操作时间-月
   */
  operTimeMonth?: null | string;
  /**
   * 操作时间-年
   */
  operTimeYear?: null | string;
  /**
   * 租户ID
   */
  tenantId?: null | string;
  /**
   * 时区
   */
  timeZone?: null | string;
  /**
   * 仓库ID
   */
  warehouseId?: null | string;
}

/**入库、出库单统计-单据量统计、入库、出库作业趋势图 */
export function statisticsOrderApi() {
  const startDate = dayjs()
    .subtract(urlMonth, 'month')
    .format('YYYY-MM-DD HH:mm:ss');
  const endTDate = dayjs().format('YYYY-MM-DD HH:mm:ss');
  return fetchWrapper<StatisticsOrderVO>(
    `${reportPath}/report/large-screen/statistics-order`,
    {
      warehouseIds,
      tenantId,
      receiptEndTime: endTDate,
      receiptStartTime: startDate,
      shipmentEndTime: endTDate,
      shipmentStartTime: startDate,
    },
    'POST',
  );
}

export type InventoryObj = {
  /** 周 */
  week: string;
  /** 数量 */
  quantity1: number;
  /** 库存维度 */
  dycDim2: string;
};

export type InventoryChartObj = {
  /** 箱类型 */
  boxInventoryChartList: InventoryObj[];
  /** sku 类型 */
  skuInventoryChartList: InventoryObj[];
};

/**库存趋势图 - 马来仓 */
export function statisticsInventoryChartApi() {
  const startDate = dayjs()
    .subtract(urlMonth, 'month')
    .format('YYYY-MM-DD HH:mm:ss');
  const endTDate = dayjs().format('YYYY-MM-DD HH:mm:ss');
  return fetchWrapper<InventoryChartObj>(
    `${reportPath}/report/large-screen/statistics-statisticsInventoryChart`,
    {
      warehouseIds,
      tenantId,
      receiptEndTime: endTDate,
      receiptStartTime: startDate,
      shipmentEndTime: endTDate,
      shipmentStartTime: startDate,
    },
    'POST',
  );
}
